package com.advertising.permissions.controls;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.advertising.permissions.biz.IUserService;
import com.advertising.permissions.common.HasParent;
import com.advertising.permissions.model.Resources;
import com.advertising.permissions.model.Users;
import com.advertising.permissions.tree.Utils;
import com.tgyt.framework.controls.struts2.BaseTg;

/**
 * @ClassName: LoginTg
 * @Description: 用户登录
 * 
 */
@Scope("prototype")
@Controller(value = "loginTgControl")
public class LoginTg extends BaseTg {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String agentname;
	private String username;
	private String password;
	@Autowired
	private IUserService userService;

	/**
	 * @Title: login
	 * @Description: 用户登录方法
	 * @param @return
	 * @return String
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public String login() {
		if(!StringUtils.isNotEmpty(username)||!StringUtils.isNotEmpty(password)){
			return ERROR;
		}
		UsernamePasswordToken token=null;
		List<Users> userList = null;
		if(agentname == null||"".equals(agentname)){
			token = new UsernamePasswordToken(username, password, Boolean.valueOf(rememberMe));
		}else{
			userList = userService.findList("from Users where logonid ='"+agentname+"'");
			for (int i = 0; i < userList.size(); i++) {
				System.out.println(userList.get(i).getLogonid());
				token = new UsernamePasswordToken(userList.get(i).getLogonid(), userList.get(i).getPassword(), Boolean.valueOf(rememberMe));
			}
		}
		Subject user = SecurityUtils.getSubject();
		
		try {
			user.login(token);
			HasParent.isHasParent();
			List<Resources> list = (List<Resources>) SecurityUtils.getSubject().getSession().getAttribute("resourceList");
			
			HasParent.returnCurrentObj(null,request);
			Session session = user.getSession();
			session.setAttribute("loginuser", userService.login(username, password));
			session.setAttribute("treescript", Utils.getLeft(list));
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("验证出错！");
			return "error";
		}
		

	}

	public String logout() {
		SecurityUtils.getSubject().logout();
		return "error";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private String rememberMe;

	public String getRememberMe() {
		return rememberMe;
	}

	public void setRememberMe(String rememberMe) {
		this.rememberMe = rememberMe;
	}

	/**
	 * @return the agentname
	 */
	public String getAgentname() {
		return agentname;
	}

	/**
	 * @param agentname
	 *            the agentname to set
	 */
	public void setAgentname(String agentname) {
		this.agentname = agentname;
	}
}
